package com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.LinearLayout;


import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;

import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyAdapter.CarShopActivityAdapter;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.ListViewForScrollView;
import com.diudiutechnology.www.yadiudiu.util.view.MyScrollView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/23.
 * 购车店铺详情
 */

public class CarShopActivity extends Activity implements View.OnClickListener ,MyScrollView.Callbacks{
    @Bind(R.id.retur)//返回
            LinearLayout retur;
    @Bind(R.id.house_layout)//返回
     LinearLayout house_layout;
    @Bind(R.id.fixed_position)//
    LinearLayout fixed_position;//固定house_layout位置

    @Bind(R.id.scroll_view)
    MyScrollView scroll_view;

    @Bind(R.id.buyacar_list)
    ListViewForScrollView buyacar_list;//listview
    private List<BuyACarFragmentModel> buycarlist;
    CarShopActivityAdapter adapter;

    @Bind(R.id.ad_pager)//轮播
            ViewPager adPager;
    @Bind(R.id.ad_layer)//圆圈
            LinearLayout adLayer;
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };
    Context context;
    int  pos=0;//初始化scrollView高度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_shop);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        List<String> getinforresult = new ArrayList<>();
        getinforresult.add("1");
        getinforresult.add("1");
        getinforresult.add("1");
        getadvertising(getinforresult.size(), getinforresult);


        scroll_view.setCallbacks(this);
        scroll_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (pos<2){
                    scroll_view.scrollTo(0,0);
                    pos++;
                }
                onScrollChanged(scroll_view.getScrollY());
            }
        });
        adapter=new CarShopActivityAdapter(context);
        buyacar_list.setAdapter(adapter);
        buyacar_list.setOnItemClickListener(clickListener);
        initData();
    }
    AdapterView.OnItemClickListener clickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
              startActivity(new Intent(context, WashCarDetailsActivity.class));
        }
    };
    //数据
    private void  initData(){
        buycarlist=new ArrayList<>();


            BuyACarFragmentModel model=new BuyACarFragmentModel();
            model.setName("东风标致4S店重庆店");
            model.setStar("2");
            model.setLet("标致508");
            model.setMoney1("29.67");
            model.setMoney2("25.00");
            model.setDistance("2.58");
            model.setDate("20");
            model.setPraise("200");
            buycarlist.add(model);
            model=new BuyACarFragmentModel();
            model.setName("广汽传祺广汽传祺广汽传祺祺");
            model.setStar("3");
            model.setLet("传祺GS8");
            model.setMoney1("26.97");
            model.setMoney2("25.00");
            model.setDistance("3.25");
            model.setDate("1.5万");
            model.setPraise("1.8万");
            buycarlist.add(model);


           model=new BuyACarFragmentModel();
            model.setName("重庆南岸白鹤路店");
            model.setStar("2");
            model.setLet("标准洗车");
            model.setMoney1("25");
            model.setMoney2("20");
            model.setDistance("2.25");
            model.setDate("20");
            model.setPraise("200");
            buycarlist.add(model);
            model=new BuyACarFragmentModel();
            model.setName("重庆南岸白鹤路店");
            model.setStar("2");
            model.setLet("标准洗车");
            model.setMoney1("25");
            model.setMoney2("20");
            model.setDistance("2.25");
            model.setDate("1.5万");
            model.setPraise("1.5万");
            buycarlist.add(model);
            model=new BuyACarFragmentModel();
            model.setName("重庆南岸白鹤路店");
            model.setStar("2");
            model.setLet("标准洗车");
            model.setMoney1("25");
            model.setMoney2("20");
            model.setDistance("3.21");
            model.setDate("20");
            model.setPraise("200");
            buycarlist.add(model);
            adapter.addItem(buycarlist);
        }


    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }

    @Override
    public void onScrollChanged(int scrollY) {
        house_layout.setTranslationY(Math.max(fixed_position.getTop(), scrollY));
    }

    @Override
    public void onDownMotionEvent() {

    }

    @Override
    public void onUpOrCancelMotionEvent() {

    }
}

